package com.zwps.biz.domain.tool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.Function;
import cn.hutool.core.util.StrUtil;

public class TreeCodeTool {

    public final static String TREE_CODE_TAG = "A";

    public static String generate(String parentTreeCode, long sort) {
        if (parentTreeCode == null) {
            return TREE_CODE_TAG + sort;
        }
        return parentTreeCode + TREE_CODE_TAG + sort;
    }

    /**
     * 生成同级递增的树编码
     * 
     * @Title: generate
     * @Description:
     * @param parentTreeCode   上级树编码
     * @param peerSortLastDept 当前同级存在的数字最大的一个树编码
     * @return String
     * @author lyh
     * @date 2023年5月25日 上午11:45:45
     */
    public static String generateIncremental(String parentTreeCode, String peerSortLastDept) {
        return generate(parentTreeCode, peerSortLastDept == null ? 1 : subLastTreeCodeNum(peerSortLastDept) + 1);
    }

    /**
     * 截取最后一个编码的数字
     * 
     * @Title: subLastTreeCodeNum
     * @Description:
     * @param treeCode
     * @return int
     * @author lyh
     * @date 2023年5月25日 上午11:42:47
     */
    public static int subLastTreeCodeNum(String treeCode) {
        return Integer.valueOf(treeCode.substring(treeCode.lastIndexOf(TREE_CODE_TAG) + TREE_CODE_TAG.length()));
    }

    /**
     * 拆分一个树编码并返回指定过滤编码的所有下级编码组合
     * @Title: splitTreeCodeAssembly
     * @Description: 
     * @param treeCode
     * @param filterTreeCode
     * @return ArrayList<String>
     * @author lyh
     * @date 2023年8月21日 下午6:58:15
     */
    public static ArrayList<String> splitTreeCodeAssembly(String treeCode, String filterTreeCode) {
        ArrayList<String> list = new ArrayList<String>();
        if (treeCode.equals(filterTreeCode)) {
            return list;
        }
        String assembly = "";
        for (int i = 0; i < treeCode.length(); i++) {
            char c = treeCode.charAt(i);
            if (TREE_CODE_TAG.equals(c + "")) {
                if (!"".equals(assembly) && !filterTreeCode.startsWith(assembly)) {
                    list.add(assembly);
                }
            }
            assembly += c;
        }
        list.add(assembly);
        return list;
    }
    
    public static HashSet<String> splitTreeCodesAssembly(List<String> treeCodes) {
        HashSet<String> set = new HashSet<String>();
        if (treeCodes.isEmpty()) {
            return set;
        }
        for (String treeCode : treeCodes) {
            String assembly = "";
            for (int i = 0; i < treeCode.length(); i++) {
                char c = treeCode.charAt(i);
                if (TREE_CODE_TAG.equals(c + "")) {
                    if (!"".equals(assembly) ) {
                        set.add(assembly);
                    }
                }
                assembly += c;
            }
            set.add(assembly);
        }
        return set;
    }
    
    public static <T> HashSet<String> splitTreeCodesAssembly(List<T> objs, Function<T, String> getTreeCodeFun) {
        HashSet<String> set = new HashSet<String>();
        if (objs.isEmpty()) {
            return set;
        }
        for (T obj : objs) {
            String treeCode = getTreeCodeFun.apply(obj);
            if (StrUtil.isEmpty(treeCode)) {
                continue;
            }
            String assembly = "";
            for (int i = 0; i < treeCode.length(); i++) {
                char c = treeCode.charAt(i);
                if (TREE_CODE_TAG.equals(c + "")) {
                    if (!"".equals(assembly)) {
                        set.add(assembly);
                    }
                }
                assembly += c;
            }
            set.add(assembly);
        }
        return set;
    }
}
